package com.zkthink.ceres.applet.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zkthink.base.R;
import com.zkthink.ceres.applet.comment.domain.entity.AppletComment;
import com.zkthink.ceres.applet.comment.service.AppletCommentService;
import com.zkthink.ceres.applet.common.domin.PageDate;
import com.zkthink.ceres.applet.common.domin.vo.PageDateTypeIdVo;
import com.zkthink.ceres.applet.homePage.domain.vo.FindServerListVo;
import com.zkthink.ceres.applet.product.mapper.AppletProductMapper;
import com.zkthink.ceres.applet.server.domain.vo.FindServerDetailVo;
import com.zkthink.ceres.applet.server.domain.vo.PageDateCommentVo;
import com.zkthink.ceres.applet.server.domain.vo.ServerDetailsVo;
import com.zkthink.ceres.applet.server.mapper.AppletStoreServerMapper;
import com.zkthink.ceres.applet.server.mapper.AppletVehiclesServerMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zkthink.ceres.applet.server.domain.entity.AppletServer;
import com.zkthink.ceres.applet.server.mapper.AppletServerMapper;
import com.zkthink.ceres.applet.server.service.AppletServerService;

/**

@Author cmz
@Date 2024-04-30 13:04
*/
@Service
public class AppletServerServiceImpl extends ServiceImpl<AppletServerMapper, AppletServer> implements AppletServerService{

    @Autowired
    private AppletServerMapper appletServerMapper;

    @Autowired
    private AppletProductMapper appletProductMapper;
    
    @Autowired
    private AppletCommentService appletCommentService;
    
    @Autowired
    private AppletStoreServerMapper appletStoreServerMapper;
    
    @Autowired
    private AppletVehiclesServerMapper appletVehiclesServerMapper;
    
    @Override
    public R findByPage(PageDateTypeIdVo pageDateTypeIdVo) {
        QueryWrapper<AppletServer> queryWrapper = new QueryWrapper<>();
        if (pageDateTypeIdVo.getKeyword()!=null){
            queryWrapper.like("title",pageDateTypeIdVo.getKeyword());
        }
        if (pageDateTypeIdVo.getTypeId()!=null){
            queryWrapper.eq("server_category_id",pageDateTypeIdVo.getTypeId());
        }
        Page<AppletServer> page = new Page<>(pageDateTypeIdVo.getPageNum(), pageDateTypeIdVo.getPageSize());
        Page<AppletServer> appletServerPage = appletServerMapper.selectPage(page, queryWrapper);
        return R.success(appletServerPage);
    }

    @Override
    public R serverDetail(FindServerDetailVo findServerDetailVo) {
        QueryWrapper<AppletServer> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",findServerDetailVo.getProductId());
        AppletServer appletServer = appletServerMapper.selectOne(queryWrapper);
        ServerDetailsVo serverDetailsVo = new ServerDetailsVo();
        //服务信息
        serverDetailsVo.setAppletServer(appletServer);

        PageDateCommentVo pageDateCommentVo = new PageDateCommentVo();
        pageDateCommentVo.setOrderType(0);
        pageDateCommentVo.setProductId(findServerDetailVo.getProductId());

        List<AppletComment> commentList = (List<AppletComment>) appletCommentService.findOneCommentById(pageDateCommentVo).getData();
        //评论信息
        serverDetailsVo.setCommentList(commentList);

//        if(findServerDetailVo.getStoreId()!=null){
//            //店铺id不为空的时候
//            //查询金额数据
//            QueryWrapper<AppletStoreServer> appletStoreServerQueryWrapper = new QueryWrapper<>();
//            appletStoreServerQueryWrapper.eq("server_id",findServerDetailVo.getProductId());
//            appletStoreServerQueryWrapper.eq("store_id",findServerDetailVo.getStoreId());
//            
//            //商铺、服务中间表数据
//            AppletStoreServer appletStoreServer = appletStoreServerMapper.selectOne(appletStoreServerQueryWrapper);
//            if (appletStoreServer!=null){
//                serverDetailsVo.setStorePrice(appletStoreServer.getStorePrice());
//                serverDetailsVo.setGroupBuyPrice(appletStoreServer.getGroupBuyPrice());
//            }
//        }
//        else if (findServerDetailVo.getVehiclesId()!=null){
//            //车辆id不为空的时候
//            //查询金额数据
//            QueryWrapper<AppletVehiclesServer> appletVehiclesServerQueryWrapper = new QueryWrapper<>();
//            appletVehiclesServerQueryWrapper.eq("vehicles_id",findServerDetailVo.getProductId());
//            appletVehiclesServerQueryWrapper.eq("server_id",findServerDetailVo.getProductId());
//            AppletVehiclesServer appletVehiclesServer = appletVehiclesServerMapper.selectOne(appletVehiclesServerQueryWrapper);
//            if (appletVehiclesServer!=null){
//                serverDetailsVo.setStorePrice(appletVehiclesServer.getStorePrice());
//                serverDetailsVo.setGroupBuyPrice(appletVehiclesServer.getGroupBuyPrice());
//            }
//        }
        return R.success(serverDetailsVo);
    }

    @Override
    public R findServerOrProduct(PageDateTypeIdVo pageDateTypeIdVo) {
        
        if (pageDateTypeIdVo.getTypeName().equals("服务分类")){
            //服务分类
//            QueryWrapper<AppletServer> queryWrapper = new QueryWrapper<>();
//            if (pageDateTypeIdVo.getKeyword()!=null){
//                queryWrapper.like("title",pageDateTypeIdVo.getKeyword());
//            }
//            if (pageDateTypeIdVo.getTypeId()!=null){
//                queryWrapper.eq("server_category_id",pageDateTypeIdVo.getTypeId());
//            }
            
            Page<FindServerListVo> appletServerPage = new Page<>(pageDateTypeIdVo.getPageNum(),pageDateTypeIdVo.getPageSize());
            Page<FindServerListVo> selectPage = appletServerMapper.selectAllPricePage(appletServerPage,pageDateTypeIdVo);
            
            
            /*List<Map<String,Object>> list = new ArrayList<>();
            for (AppletServer record : selectPage.getRecords()) {
                Map<String,Object> map = new HashMap<>();
                map.put("id",record.getId());
                map.put("name",record.getTitle());
                map.put("img",record.getImg());
                list.add(map);
            }
            Page page = copyPage(selectPage);
            page.setRecords(list);*/
            return R.success(selectPage);
            
        }else {
            //产品分类
            /*QueryWrapper<AppletProduct> queryWrapper = new QueryWrapper<>();
            if (pageDateTypeIdVo.getKeyword()!=null){
                queryWrapper.like("product_name",pageDateTypeIdVo.getKeyword());
            }
            if (pageDateTypeIdVo.getTypeId()!=null){
                queryWrapper.eq("category_id",pageDateTypeIdVo.getTypeId());
            }*/
            
            //查询所有的产品
            Page<FindServerListVo> appletServerPage = new Page<>(pageDateTypeIdVo.getPageNum(),pageDateTypeIdVo.getPageSize());
            Page<FindServerListVo> appletProductPage = appletProductMapper.selectAllPricePage(appletServerPage,pageDateTypeIdVo);
            /*List<Map<String,Object>> list = new ArrayList<>();
            for (AppletProduct record : appletProductPage.getRecords()) {
                Map<String,Object> map = new HashMap<>();
                map.put("id",record.getId());
                map.put("name",record.getProductName());
                map.put("img",record.getProductImg());
                list.add(map);
            }
            Page page = copyPage(appletProductPage);
            page.setRecords(list);*/
            return R.success(appletProductPage);
        }
    }

    @Override
    public R findRecoProduct(PageDate pageDate) {
        Page<FindServerListVo> appletServerPage = new Page<>(pageDate.getPageNum(),pageDate.getPageSize());
        Page<FindServerListVo> appletProductPage = appletProductMapper.findRecoProduct(appletServerPage);
        return R.success(appletProductPage);
    }


}
